/*
 * Copyright (c) 2025, 廖树财, <study_2030@163.com>
 *
 * PEE2.0-YCYW-HY is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2     
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#pragma once
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include "zlog.h"
#include "ctrl.h"
#include "JsonParser.h"
#include "common.h"
#include <openssl/ssl.h>
#include <openssl/engine.h>

typedef struct MemoryStruct
{
    char *memory;
    long rescode;
    size_t size;
} MemoryStruct;

typedef struct strProgressInfo
{
    int action;
    double Progress;    // 存储进度
    curl_off_t Bytes;   // 字节数
    curl_off_t Bytes_bak;   // 字节数
    char fileid[100];   // 文件ID
    char filename[200]; // 文件名
    char md5[80];       // 文件校验
    long filesize;      // 文件大小
    int ret;            // 返回下载结果
    int stop;           // 停止下载
} ProgressInfo;

extern int HTTP_SSL;
extern char *CLIENT_SIGN_CERT_PATH;
extern char *CLIENT_SIGN_KEY_PATH;
extern char *CLIENT_ENC_CERT_PATH;
extern char *CLIENT_ENC_KEY_PATH;
extern char *CLIENT_CA_CERT_PATH;

extern ProgressInfo **progress_info_ptr;

int http_init();

int http_init_url(char* ip, int HTTP_SSL);

void http_free();

int httpFileServerLoginWithUser(char *access_token, const char *user, const char *password, const char *clientuser, const char *clientpw);

int httpFileServerLoginWithUser_v1(ST_IOTA_LOGIN_RES_V1 *login, const char *user, const char *password, const char *clientuser, const char *clientpw);

int httpFileServerGetHeader(char *access_token, ST_IOTA_AUTH_RES *res);

int httpFileServerGetHeader_v1(char *access_token, ST_IOTA_AUTH_RES *res);

int httpFileServerUploadFile_v1(char *filename, ST_IOTA_AUTH_RES_V1 *header, ST_IOTA_FILE_RES *res, ProgressInfo *info);

int httpFileServerUploadFileReturn(char *res, char *access_token);

int httpFileServerDownload(ST_IOTA_AUTH_RES *header, char *fileId, char *frilename, char *lurl, ProgressInfo *info);

int httpFileServerDownload_v1(ST_IOTA_AUTH_RES_V1 *header, char *fileId, char *frilename, char *lurl, ProgressInfo *info);

CURLcode curl_post_req(char *url, char *postParams, MemoryStruct *response, char *listRequestHeader, int content_type, bool auth, char *access_token, bool bResponseIsWithHeaderData, ProgressInfo *info);

size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data);

int curl_get_req(char *url, MemoryStruct *response, char *listRequestHeader, bool isFile, char *filename, bool bResponseIsWithHeaderData, bool auth, char *access_token, ProgressInfo *info);

int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);

CURLcode sslCtxCallback(CURL *curl, void *sslctx, void *userptr);

size_t writeFileCallback(void *contents, size_t size, size_t nmemb, void *userp);

int httpSubDeviceAuthReq(char *psContent);
